<?php
namespace TwinPeaks\Core;

class CSysException extends CBaseException
{
	const CANNOT_REDEFINE_PATH_ALIAS = 1;
	const INTERFACE_NOT_IMPLEMENTED = 2;
	const CONTROLLER_NO_METHOD = 3;
	const PATH_ALIAS_NOT_DEFINED = 4;
	
	function __construct($message, $code)
	{
		parent::__construct($message, $code);
		$this->setClass(SYS_EXCEPTION_CLASS);
	}
	
	/**
	 * @return CSysException
	 */
	static function pathAliasDefined($alias)
	{
		return new static(sprintf('Cannot redefine path alias @%s', $alias),
			self::CANNOT_REDEFINE_PATH_ALIAS);
	}
	
	/**
	 * @return CSysException
	 */
	static function interfaceNotImplemented($object, $interfaceName)
	{
		return new static(sprintf('Class %s must implement interface %s',
			get_class($object), $interfaceName), self::INTERFACE_NOT_IMPLEMENTED);
	}
	
	/**
	 * @return CSysException
	 */
	static function controllerNoMethod($ctrl, $methodName)
	{
		return new static(sprintf('Controller method %s::%s is not defined',
			get_class($ctrl), $methodName), self::CONTROLLER_NO_METHOD);
	}
	
	
	/**
	 * @return void
	 */
	static function invalidReqURI($requestURI)
	{
		throw new static();
	}

	/**
	 * @return void
	 */
	static function pathAliasNotDefined($alias)
	{
		return new static(sprintf('Path alias %s is not defined', $alias),
			self::PATH_ALIAS_NOT_DEFINED);
	}
	
	/**
	 * @throwException CException
	 */
	static function encodingConversionFailed($string)
	{
		$excep = new static(sprintf("Either the iconv or mbstring extension was not installed. Failed to convert string %s",
			$string), ERR_SYSTEM
		);
		$excep->type = null;
		throw $excep;		
	}
	
	/**
	 * 
	 */
	static function accessDenied($rule)
	{
		$excep = new static(sprintf('Secutiry violation'), ERR_SECURITY_VIOLATION);
		
		return $excep;
	}
	
	/**
	 * @return
	 */
	static function filterWrongDatatypeDef($def)
	{
		$excep = new static("Wrong data type definition ${def}");
		
		return $excep;
	}
}
